package com.yaolaizaixian.merchant.home;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.yaolaizaixian.merchant.R;
import com.yaolaizaixian.merchant.base.BaseActivity;
import com.yaolaizaixian.merchant.utils.GlideCatchUtil;
import com.yaolaizaixian.merchant.utils.MyUtils;
import com.yaolaizaixian.merchant.utils.SharedPreferencesUtil;
import com.yaolaizaixian.merchant.utils.Toasty;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.drakeet.materialdialog.MaterialDialog;

/**
 * Created by Administrator on 2017/2/23.
 */
public class SttingActivity extends BaseActivity {

    // user your appid the key.
    private static final String APP_ID = "2882303761517556684";

    private static final String TAG = "SttingActivity";
    @BindView(R.id.ll_back)
    AutoLinearLayout llBack;
    @BindView(R.id.tv_title_text)
    TextView tvTitleText;
    @BindView(R.id.tb_tuisong)
    ToggleButton tbTuisong;
    @BindView(R.id.rL_close_push)
    AutoRelativeLayout rLClosePush;
    @BindView(R.id.tv_string_huancun)
    TextView tvStringHuancun;
    @BindView(R.id.rL_clean)
    AutoRelativeLayout rLClean;
    @BindView(R.id.tv_setting_ban)
    TextView tvSettingBan;
    @BindView(R.id.tv_setting_data)
    TextView tvSettingData;
    @BindView(R.id.rl_success)
    AutoRelativeLayout rlSuccess;
    @BindView(R.id.rl_confirm)
    AutoRelativeLayout rlConfirm;
    @BindView(R.id.rl_cancle)
    AutoRelativeLayout rlCancle;

    @Override
    public void initView() {

        setContentView(R.layout.activity_stting);

    }

    @Override
    public void initData() {

        tvTitleText.setText("设置");

        String version = String.valueOf(MyUtils.getVersion(SttingActivity.this));
        String version1 = version.substring(0, 1);
        String version2 = version.substring(1, 2);
        String version3 = version.substring(2, 3);
        tvSettingBan.setText("耀莱商家" + version1 + "." + version2 + "." + version3);
        //更新日期
        tvSettingData.setText("发版日期: 2017.05.12");

        if (!TextUtils.isEmpty(GlideCatchUtil.getCacheSize()) && !"0.0Byte".equals(GlideCatchUtil.getCacheSize())) {

            tvStringHuancun.setText("清理缓存" + "(" + GlideCatchUtil.getCacheSize() + ")");

        } else {

            tvStringHuancun.setText("清理缓存");
        }

        //默认的推送为打开的
        boolean push = SharedPreferencesUtil.getBoolean(getApplicationContext(), "push", true);

        if (push) {
            tbTuisong.setChecked(true);
        } else {
            tbTuisong.setChecked(false);
        }

        tbTuisong.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {
//                    SharedPreferencesUtil.saveBoolean(getApplicationContext(), "push", true);
//                    MiPushClient.registerPush(getApplicationContext(), BaseInfo.APP_ID, BaseInfo.APP_KEY);
                    //打开推送
//                    JPushInterface.resumePush(getApplicationContext());
                    Log.e(TAG, "打开");

                } else {
//                    SharedPreferencesUtil.saveBoolean(getApplicationContext(), "push", false);
//                    MiPushClient.unregisterPush(getApplicationContext());
                    //关闭推送
//                    JPushInterface.stopPush(getApplicationContext());
                    Log.e(TAG, "关闭");
                }
            }
        });
    }


    @OnClick({R.id.rL_close_push, R.id.rL_clean, R.id.ll_back,R.id.rl_success, R.id.rl_confirm, R.id.rl_cancle})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.rL_close_push:

                break;

            //清理缓存
            case R.id.rL_clean:

                if (!TextUtils.isEmpty(GlideCatchUtil.getCacheSize()) && !"0.0Byte".equals(GlideCatchUtil.getCacheSize())) {

                    final MaterialDialog mMaterialDialog = new MaterialDialog(SttingActivity.this);
                    mMaterialDialog.setMessage("是否清空缓存？");

                    mMaterialDialog.setPositiveButton("确定", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            mMaterialDialog.dismiss();

                            if (GlideCatchUtil.clearCacheDiskSelf()) {

                                Toasty.success(SttingActivity.this, "清理成功", Toast.LENGTH_SHORT, true).show();

                                tvStringHuancun.setText("清理缓存");

                            } else {
                                Toasty.error(SttingActivity.this, "清理失败", Toast.LENGTH_SHORT, true).show();
                            }


                        }
                    });

                    mMaterialDialog.setNegativeButton("取消", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            mMaterialDialog.dismiss();
                        }
                    });

                    mMaterialDialog.show();

                } else {

                    Toasty.error(SttingActivity.this, "没有可以清理的缓存", Toast.LENGTH_SHORT, true).show();
                }

                break;


            case R.id.ll_back:

                finish();

                break;

            case R.id.rl_success:

                Intent intent=new Intent(this,QunyiErweimaActivity.class);
                startActivity(intent);

                break;
            case R.id.rl_confirm:

                Intent intent1=new Intent(this,ConfirmActivity.class);
                startActivity(intent1);

                break;
            case R.id.rl_cancle:

                Intent intent2=new Intent(this,CancelOrderActivity.class);
                startActivity(intent2);
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


}
